namespace GemBox.Spreadsheet
{
    using System;
    using System.Collections;
    using System.Reflection;

    public class NamedRangeCollection : IEnumerable
    {
        private ArrayList namedRanges;
        private ArrayList namesList;
        private ExcelWorksheet worksheet;

        internal NamedRangeCollection(ExcelWorksheet worksheet)
        {
            this.namesList = new ArrayList();
            this.namedRanges = new ArrayList();
            this.worksheet = worksheet;
        }

        internal NamedRangeCollection(ExcelWorksheet worksheet, NamedRangeCollection sourceNamedRanges)
        {
            this.namesList = new ArrayList();
            this.namedRanges = new ArrayList();
            this.worksheet = worksheet;
            for (int i = 0; i < sourceNamedRanges.NamedRanges.Count; i++)
            {
                NamedRange range = sourceNamedRanges.NamedRanges[i] as NamedRange;
                this.Add(range.Options, range.Name, range.Range, range.GlobalName);
            }
        }

        public void Add(string name, CellRange range)
        {
            this.Add(new object[0], name, range, false);
        }

        public void Add(string name, CellRange range, bool globalName)
        {
            this.Add(new object[0], name, range, globalName);
        }

        internal void Add(object[] options, string name, CellRange range, bool globalName)
        {
            this.namedRanges.Add(new NamedRange(this, this.namedRanges.Count, options, name, range, globalName));
            this.namesList.Add(name);
        }

        internal void DeleteInternal(int index)
        {
            this.namedRanges.RemoveAt(index);
            this.namesList.RemoveAt(index);
            this.RefreshCollectionIndexes(index);
        }

        public IEnumerator GetEnumerator()
        {
            return this.namedRanges.GetEnumerator();
        }

        internal void RefreshCollectionIndexes(int startIndex)
        {
            for (int i = startIndex; i < this.NamedRanges.Count; i++)
            {
                NamedRange range = this.NamedRanges[i] as NamedRange;
                range.index = i;
            }
        }

        public int Count
        {
            get
            {
                return this.namedRanges.Count;
            }
        }

        public NamedRange this[int index]
        {
            get
            {
                return (this.namedRanges[index] as NamedRange);
            }
        }

        public NamedRange this[string name]
        {
            get
            {
                for (int i = 0; i < this.namedRanges.Count; i++)
                {
                    NamedRange range = this.namedRanges[i] as NamedRange;
                    if (range.Name == name)
                    {
                        return range;
                    }
                }
                return null;
            }
        }

        internal ArrayList NamedRanges
        {
            get
            {
                return this.namedRanges;
            }
        }

        internal string[] Names
        {
            get
            {
                return (string[]) this.namesList.ToArray(typeof(string));
            }
        }

        internal ArrayList NamesList
        {
            get
            {
                return this.namesList;
            }
        }
    }
}
